<?php

namespace App\Admin\Controllers\Common;

use App\Models\Common\CommonParam;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ParamsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '参数';


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CommonParam());

        $grid->column('id', __('field.id'));
        $grid->column('parent_id', __('field.parent_id'));
        $grid->column('field', __('field.field'));
        $grid->column('name', __('field.name'));
        $grid->column('value', __('field.value'));
        $grid->column('remark', __('field.remark'));
        $grid->column('created_at', __('field.created_at'));
        $grid->column('updated_at', __('field.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CommonParam::findOrFail($id));

        $show->field('id', __('field.id'));
        $show->field('parent_id', __('field.parent_id'));
        $show->field('field', __('field.field'));
        $show->field('name', __('field.name'));
        $show->field('value', __('field.value'));
        $show->field('remark', __('field.remark'));
        $show->field('created_at', __('field.created_at'));
        $show->field('updated_at', __('field.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CommonParam());

        $form->select('parent_id', __('field.parent_id'))
            ->options(CommonParam::treeData());
        $form->text('field', __('field.field'));
        $form->text('name', __('field.name'));
        $form->text('value', __('field.value'));
        $form->text('remark', __('field.remark'));

        return $form;
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body(CommonParam::tree());
    }

    public function destroy($id)
    {
         $param = CommonParam::query()-find($id);
         if(!$param) {
             return response([
                 'status' => false,
                 'message' => '数据不存在',
             ]);
         }
         if ($param->field) {
             return response([
                 'status' => false,
                 'message' => '系统必须字段！不能删除',
             ]);
         }
         return parent::destroy($id);
    }
}
